package com.easystudy.listener;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import com.easystudy.video.call.Callback;
import com.easystudy.video.error.ReturnValue;
import com.easystudy.video.model.BoxInfo;
import com.easystudy.video.model.ConfigInfo;
import com.easystudy.video.model.DeviceInfo;
import com.easystudy.video.model.EventRecord;
import com.easystudy.video.model.HeartInfo;
import com.easystudy.video.model.TimeInfo;
import com.easystudy.video.service.VideoService;

import cn.hutool.core.net.NetUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @文件名称: AppStartupListener.java
 * @功能描述: 系统启动监听器
 * @版权信息: lixx2048@163.com
 * @技术交流: 961179337(QQ群)
 * @编写作者: 东为信息
 * @开发日期: 2023年11月14日
 * @历史版本: V1.0 
 * @备注信息: 可以在容器启动之后做初始化工作或销毁之前做销毁工作
 */
@Slf4j
@WebListener
public class AppStartupListener implements ServletContextListener {
	@Autowired
	private VideoService videoService;

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		log.debug("服务正在初始化...");
		
		// 启动IOT视频接入
		startIotVideoService();
	
		log.debug("服务初始化完成。");
	}
	
	private void startIotVideoService() {
		videoService.register(new Callback() {
			
			@Override
			public ReturnValue<String> onConnect(String boxId, BoxInfo boxInfo) {
				return new ReturnValue<String>();
			}
			
			@Override
			public ReturnValue<String> onDisConnect(String boxId) {
				return new ReturnValue<String>();
			}
			
			@Override
			public ReturnValue<TimeInfo> onHeart(String boxId, HeartInfo heartInfo) {
				// 获取本地时间及mac地址
				TimeInfo time = new TimeInfo();
				time.setTime(new Date());
				try {
					InetAddress address = InetAddress.getLocalHost();
					String ip = address.getHostAddress();
					time.setIp(ip);
					
					String mac = NetUtil.getMacAddress(address);
					time.setMac(mac);
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
				return new ReturnValue<TimeInfo>(time);
			}
			
			@Override
			public ReturnValue<String> onResult(String boxId, String cmdId, Integer state, 
											    String desc, String json, MultipartFile file) {
				return new ReturnValue<String>();
			}
			
			@Override
			public ReturnValue<ConfigInfo> onGetConfig(String boxId) {
				return new ReturnValue<ConfigInfo>();
			}
			
			@Override
			public ReturnValue<List<DeviceInfo>> onGetDevices(String boxId, Long pageIndex, Long pageSize) {
				List<DeviceInfo> devices = new ArrayList<DeviceInfo>();
				
				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
				
				DeviceInfo dev = new DeviceInfo();
				dev.setId("001");
				dev.setCreateTime(sf.format(new Date()));
				dev.setName("测试设备");
				dev.setRtspUrl("rtsp://192.168.50.105:8554/camera");
				dev.setUpdateTime(sf.format(new Date()));
				devices.add(dev);
				
				return new ReturnValue<List<DeviceInfo>>(devices);
			}
			
			@Override
			public ReturnValue<String> onRecord(String boxId, EventRecord record) {
				log.info("视频盒子:{} 上传录像信息:{}", boxId, record);
				return new ReturnValue<String>();
			}
		});
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		log.debug("服务正在卸载...");
		log.debug("服务卸载完成...");
	}
}
